package com.editors;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import com.data.DoubleTextField;
import com.data.IntegerTextField;
import com.data.LineData;
import com.data.Point3D;
import com.data.PolygonMesh;

public class TrackEditorLandscape extends JDialog implements ActionListener{

	int WIDTH=300;
	int BOTTOM_HEIGHT=100;
	int HEIGHT=200;
	private JButton generate;
	private JButton delete;
	
	private IntegerTextField box_num_x;
	private IntegerTextField box_num_y;


    public int side=200;
	
	
	
	PolygonMesh pm=null;
	private JPanel box_panel;
	
	boolean saved=false;
	private DoubleTextField box_x0;
	private DoubleTextField box_y0;
	
	public TrackEditorLandscape(){
		
		pm=null;
	
		setSize(WIDTH,HEIGHT+BOTTOM_HEIGHT);
		setLayout(null);
		setModal(true);
		setTitle("Create box");
		
		int r=10;
		
		box_panel=getBoxPanel();
		box_panel.setBounds(0, 0, WIDTH, HEIGHT);
        add(box_panel); 
		
		r+=HEIGHT;
		
        generate=new JButton("Create box");
        generate.setBounds(10,r,130,20);
        generate.addActionListener(this);
        
        add(generate);
        delete=new JButton("Cancel");
        delete.setBounds(150,r,100,20);
        delete.addActionListener(this);
        add(delete);
        
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        
    	//debug
		setData();
		
		setVisible(true);
	
	}
	
	private void setData() {



		box_num_x.setText(10);
		box_num_y.setText(10);

		box_x0.setText(0);
		box_y0.setText(0);
	}
	
	private JPanel getBoxPanel() {
		
		JPanel box=new JPanel();
		box.setLayout(null);
		
		int r=10;
		int column=100;
	
		
	
		JLabel jlb=new JLabel("Num X points");
		jlb.setBounds(5, r, 100, 20);
		box.add(jlb);
		box_num_x=new IntegerTextField();
		box_num_x.setBounds(column, r, 100, 20);
		box.add(box_num_x);
	
		r+=30;
		jlb=new JLabel("Num Y points");
		jlb.setBounds(5, r, 100, 20);
		box.add(jlb);
		box_num_y=new IntegerTextField();
		box_num_y.setBounds(column, r, 100, 20);
		box.add(box_num_y);
		
		
		r+=30;
		jlb=new JLabel("X0");
		jlb.setBounds(5, r, 100, 20);
		box.add(jlb);
		box_x0=new DoubleTextField();
		box_x0.setBounds(column, r, 100, 20);
		box.add(box_x0);
		
		
		r+=30;
		jlb=new JLabel("Y0");
		jlb.setBounds(5, r, 100, 20);
		box.add(jlb);
		box_y0=new DoubleTextField();
		box_y0.setBounds(column, r, 100, 20);
		box.add(box_y0);
	

		
		return box;
		
		
	}

	public void generateTemplate(){


		saved=true;



		String snumx=box_num_x.getText();
		String snumy=box_num_y.getText();

		String sx0=box_x0.getText();
		String sy0=box_y0.getText();
		
		
		if(snumx==null || snumx.length()==0 
				|| snumy==null || snumy.length()==0
				|| sx0==null || sx0.length()==0
				|| sy0==null || sy0.length()==0
				)
			return ;
		

		
		int numx=(int) box_num_x.getvalue();
		int numy=(int)box_num_y.getvalue();
		double x0=box_x0.getvalue();
		double y0=box_y0.getvalue();
		
		pm=new PolygonMesh();
		
		pm.points=new Point3D[numx*numy];
		
		
		for (int i = 0; i < numx; i++) {
			
			for (int j = 0; j < numy; j++) {
				
				
					
				int pos=pos(i,j,numx,numy);
									
				pm.points[pos]=new Point3D(x0+i*side,y0+j*side,0);
			
				
			}
			
		}
		
	
		
		//z constant
		for (int i = 0; i < numx-1; i++) {
			
			for (int j = 0; j < numy-1; j++) {
				
				
				//base z=0
				
				int pos0=pos(i,j,numx,numy);
				int pos1=pos(i+1,j,numx,numy);
				int pos2=pos(i+1,j+1,numx,numy);
				int pos3=pos(i,j+1,numx,numy);
				
				LineData ld=new LineData();
				ld.addIndex(pos0);
				ld.addIndex(pos1);
				ld.addIndex(pos2);
				ld.addIndex(pos3);
				
				pm.polygonData.add(ld);
	
				ld.setTexture_index((i+j)%2);
			}
		}	
	
		pm=PolygonMesh.simplifyMesh(pm);

	}
	
	private int pos(int i, int j,  int numx, int numy) {
		
		return (i*numy+j);
	}	

	@Override
	public void actionPerformed(ActionEvent e) {
	
		

		Object o=e.getSource();

		if(o==generate){
			
			generateTemplate();
			dispose();	
		}
		else if(o==delete){

			saved=false;
			pm=null;
			dispose();	
		}
		
	}


	
	public PolygonMesh getLandscape(){
		
		
		if(!saved) 
			return null;
		

		
	
	     return pm;
	}	
}
